﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Utils;
using Lib.Bases;

namespace MS.INV.MainData
{
    public partial class Kho_TonDau : DevExpress.XtraEditors.XtraForm
    {
        SysObj.Kho_TonDau cls = new SysObj.Kho_TonDau();

        DataTable HH = new DataTable();

        public Kho_TonDau()
        {
            InitializeComponent();
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            cls.Dispose();
            base.OnHandleDestroyed(e);
        }

        private void Kho_TonDau_Load(object sender, EventArgs e)
        {
            try
            {
                using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
                    HH = hh.LoadData();

                repHH.DataSource = HH;
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Form Load");
            }

            dtNgay.EditValue = DateTime.Now;
        }

        private void LoadData()
        {
            try
            {
                cls.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                grid.DataSource = cls.LoadData();
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Load data");
            }
        }

        private void btUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                cls.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                cls.Detail = grid.GetAcceptChanges();
                cls.OnUpdate();

                btUpdate.Enabled = false;
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Update data");
            }
        }

        private void btClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
                this.Close();
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void view_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow row = view.GetFocusedDataRow();
            if (!object.Equals(row, null))
            {
                if (e.Column.FieldName.ToLower() == "idhanghoa")
                {
                    DataRow[] rS = HH.Select("IdHangHoa=" + Cvert.ToInt32(row["IdHangHoa"], 0));
                    if (!Is.IsNullOrEmpty(rS))
                    {
                        row["MaHang"] = Cvert.ToString(rS[0]["MaHang"], "");
                        row["TenDVT"] = Cvert.ToString(rS[0]["TenDVT"], "");
                    }
                }
            }
            btUpdate.Enabled = true;
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
            btUpdate.Enabled = true;
        }
    }
}
